
package com.ebuy.mall.service.impl;

import com.ebuy.mall.controller.vo.EBuyMallShoppingCartItemVO;
import com.ebuy.mall.common.Constants;
import com.ebuy.mall.common.ServiceResultEnum;
import com.ebuy.mall.dao.EBuyMallGoodsMapper;
import com.ebuy.mall.dao.EBuyMallShoppingCartItemMapper;
import com.ebuy.mall.entity.EBuyMallGoods;
import com.ebuy.mall.entity.EBuyMallShoppingCartItem;
import com.ebuy.mall.service.EBuyMallShoppingCartService;
import com.ebuy.mall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class EBuyMallShoppingCartServiceImpl implements EBuyMallShoppingCartService {

    @Autowired
    private EBuyMallShoppingCartItemMapper EBuyMallShoppingCartItemMapper;

    @Autowired
    private EBuyMallGoodsMapper EBuyMallGoodsMapper;

    @Override
    public String saveNewBeeMallCartItem(EBuyMallShoppingCartItem EBuyMallShoppingCartItem) {
        EBuyMallShoppingCartItem temp = EBuyMallShoppingCartItemMapper.selectByUserIdAndGoodsId(EBuyMallShoppingCartItem.getUserId(), EBuyMallShoppingCartItem.getGoodsId());
        if (temp != null) {
            //已存在则修改该记录
            temp.setGoodsCount(EBuyMallShoppingCartItem.getGoodsCount());
            return updateNewBeeMallCartItem(temp);
        }
        EBuyMallGoods eBuyMallGoods = EBuyMallGoodsMapper.selectByPrimaryKey(EBuyMallShoppingCartItem.getGoodsId());
        //商品为空
        if (eBuyMallGoods == null) {
            return ServiceResultEnum.GOODS_NOT_EXIST.getResult();
        }
        int totalItem = EBuyMallShoppingCartItemMapper.selectCountByUserId(EBuyMallShoppingCartItem.getUserId()) + 1;
        //超出单个商品的最大数量
        if (EBuyMallShoppingCartItem.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //超出最大数量
        if (totalItem > Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_TOTAL_NUMBER_ERROR.getResult();
        }
        //保存记录
        if (EBuyMallShoppingCartItemMapper.insertSelective(EBuyMallShoppingCartItem) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateNewBeeMallCartItem(EBuyMallShoppingCartItem EBuyMallShoppingCartItem) {
        EBuyMallShoppingCartItem EBuyMallShoppingCartItemUpdate = EBuyMallShoppingCartItemMapper.selectByPrimaryKey(EBuyMallShoppingCartItem.getCartItemId());
        if (EBuyMallShoppingCartItemUpdate == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        //超出单个商品的最大数量
        if (EBuyMallShoppingCartItem.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        // 数量相同不会进行修改
        if (EBuyMallShoppingCartItemUpdate.getGoodsCount().equals(EBuyMallShoppingCartItem.getGoodsCount())) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        // userId不同不能修改
        if (!EBuyMallShoppingCartItem.getUserId().equals(EBuyMallShoppingCartItemUpdate.getUserId())) {
            return ServiceResultEnum.NO_PERMISSION_ERROR.getResult();
        }
        EBuyMallShoppingCartItemUpdate.setGoodsCount(EBuyMallShoppingCartItem.getGoodsCount());
        EBuyMallShoppingCartItemUpdate.setUpdateTime(new Date());
        //修改记录
        if (EBuyMallShoppingCartItemMapper.updateByPrimaryKeySelective(EBuyMallShoppingCartItemUpdate) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public EBuyMallShoppingCartItem getNewBeeMallCartItemById(Long newBeeMallShoppingCartItemId) {
        return EBuyMallShoppingCartItemMapper.selectByPrimaryKey(newBeeMallShoppingCartItemId);
    }

    @Override
    public Boolean deleteById(Long shoppingCartItemId, Long userId) {
        EBuyMallShoppingCartItem EBuyMallShoppingCartItem = EBuyMallShoppingCartItemMapper.selectByPrimaryKey(shoppingCartItemId);
        if (EBuyMallShoppingCartItem == null) {
            return false;
        }
        //userId不同不能删除
        if (!userId.equals(EBuyMallShoppingCartItem.getUserId())) {
            return false;
        }
        return EBuyMallShoppingCartItemMapper.deleteByPrimaryKey(shoppingCartItemId) > 0;
    }

    @Override
    public List<EBuyMallShoppingCartItemVO> getMyShoppingCartItems(Long newBeeMallUserId) {
        List<EBuyMallShoppingCartItemVO> EBuyMallShoppingCartItemVOS = new ArrayList<>();
        List<EBuyMallShoppingCartItem> EBuyMallShoppingCartItems = EBuyMallShoppingCartItemMapper.selectByUserId(newBeeMallUserId, Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER);
        if (!CollectionUtils.isEmpty(EBuyMallShoppingCartItems)) {
            //查询商品信息并做数据转换
            List<Long> newBeeMallGoodsIds = EBuyMallShoppingCartItems.stream().map(EBuyMallShoppingCartItem::getGoodsId).collect(Collectors.toList());
            List<EBuyMallGoods> eBuyMallGoods = EBuyMallGoodsMapper.selectByPrimaryKeys(newBeeMallGoodsIds);
            Map<Long, EBuyMallGoods> newBeeMallGoodsMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(eBuyMallGoods)) {
                newBeeMallGoodsMap = eBuyMallGoods.stream().collect(Collectors.toMap(EBuyMallGoods::getGoodsId, Function.identity(), (entity1, entity2) -> entity1));
            }
            for (EBuyMallShoppingCartItem EBuyMallShoppingCartItem : EBuyMallShoppingCartItems) {
                EBuyMallShoppingCartItemVO EBuyMallShoppingCartItemVO = new EBuyMallShoppingCartItemVO();
                BeanUtil.copyProperties(EBuyMallShoppingCartItem, EBuyMallShoppingCartItemVO);
                if (newBeeMallGoodsMap.containsKey(EBuyMallShoppingCartItem.getGoodsId())) {
                    EBuyMallGoods EBuyMallGoodsTemp = newBeeMallGoodsMap.get(EBuyMallShoppingCartItem.getGoodsId());
                    EBuyMallShoppingCartItemVO.setGoodsCoverImg(EBuyMallGoodsTemp.getGoodsCoverImg());
                    String goodsName = EBuyMallGoodsTemp.getGoodsName();
                    // 字符串过长导致文字超出的问题
                    if (goodsName.length() > 28) {
                        goodsName = goodsName.substring(0, 28) + "...";
                    }
                    EBuyMallShoppingCartItemVO.setGoodsName(goodsName);
                    EBuyMallShoppingCartItemVO.setSellingPrice(EBuyMallGoodsTemp.getSellingPrice());
                    EBuyMallShoppingCartItemVOS.add(EBuyMallShoppingCartItemVO);
                }
            }
        }
        return EBuyMallShoppingCartItemVOS;
    }
}
